package com.test.mytest.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServerServlet extends HttpServlet {
	 // TODO 任务队列 ,还没实现 
    ConcurrentLinkedQueue<String> taskQueue = new ConcurrentLinkedQueue<String>();
    //请求响应队列
    ConcurrentLinkedQueue<AsyncContext> contextQueue = new ConcurrentLinkedQueue<AsyncContext>();
     
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
    	System.out.println("获取到客户端的get请求");
    	doPost(req, resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	String deviceIdentifier = req.getParameter("deviceIdentifier");
    	String cardIdentifier=req.getParameter("cardIdentifier");
    	System.out.println("==================================");
    	
    	System.out.println("req.getParameterMap======"+req.getParameterMap());
    	System.out.println("deviceIdentifier="+deviceIdentifier+"|===cardIdentifier="+cardIdentifier);
    	System.out.println("==================================");
    	PrintWriter writer = resp.getWriter();
    	writer.write("{\"code\":0}");
    	writer.flush();
    	
//        req.getSession().setAttribute("111", "111");
//    	req.setCharacterEncoding("UTF-8");
//    	resp.setCharacterEncoding("UTF-8");
//    	AsyncContext context = req.startAsync();
//    	resp.getWriter().flush();
//    	new Thread(new Runnable() {
//			@Override
//			public void run() {
//				try {
//					while (true) {
//						Thread.sleep(1000);
//						ServletResponse response = context.getResponse();
//						PrintWriter writer = response.getWriter();
//						writer.println("1111");
//						writer.flush();
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		}).start();
    	
         
    }
}
